package com.wsd.common;

import com.tencentcloudapi.common.Credential;
import com.tencentcloudapi.common.profile.ClientProfile;
import com.tencentcloudapi.common.profile.HttpProfile;
import com.tencentcloudapi.common.exception.TencentCloudSDKException;

import com.tencentcloudapi.ocr.v20181119.OcrClient;

import com.tencentcloudapi.ocr.v20181119.models.BankCardOCRRequest;
import com.tencentcloudapi.ocr.v20181119.models.BankCardOCRResponse;
import com.tencentcloudapi.ocr.v20181119.models.IDCardOCRRequest;
import com.tencentcloudapi.ocr.v20181119.models.IDCardOCRResponse;

public class CardOCR {
    public static String idCardOCR(String imageBase64) {
        try{
            Credential cred = new Credential("AKIDShiTzFEtNb7OPAzvymMktoTr0xe5iHzR", "MTO5jCPVMyKWTBhxBMRQ1VsuobHqIPQK");
            HttpProfile httpProfile = new HttpProfile();
            httpProfile.setEndpoint("ocr.tencentcloudapi.com");
            ClientProfile clientProfile = new ClientProfile();
            clientProfile.setHttpProfile(httpProfile);
            OcrClient client = new OcrClient(cred, "ap-shanghai", clientProfile);
            String config = "{\\\"CropIdCard\\\": true, \\\"CropPortrait\\\": true}";
            String params = "{\"ImageBase64\": \"" + imageBase64 + "\", \"Config\": \"" + config + "\"}";
            IDCardOCRRequest req = IDCardOCRRequest.fromJsonString(params, IDCardOCRRequest.class);
            IDCardOCRResponse resp = client.IDCardOCR(req);
            return IDCardOCRResponse.toJsonString(resp);
        } catch (TencentCloudSDKException e) {
            return e.toString();
        }
    }

    public static String bankCardOCR(String imageBase64) {
        try {
            Credential cred = new Credential("AKIDShiTzFEtNb7OPAzvymMktoTr0xe5iHzR", "MTO5jCPVMyKWTBhxBMRQ1VsuobHqIPQK");
            HttpProfile httpProfile = new HttpProfile();
            httpProfile.setEndpoint("ocr.tencentcloudapi.com");
            ClientProfile clientProfile = new ClientProfile();
            clientProfile.setHttpProfile(httpProfile);
            OcrClient client = new OcrClient(cred, "ap-shanghai", clientProfile);
            String params = "{\"ImageBase64\": \"" + imageBase64 + "\"}";
            BankCardOCRRequest req = BankCardOCRRequest.fromJsonString(params, BankCardOCRRequest.class);
            BankCardOCRResponse resp = client.BankCardOCR(req);
            return BankCardOCRResponse.toJsonString(resp);
        } catch (TencentCloudSDKException e) {
            return e.toString();
        }
    }

}
